<template>
  <div>
    <div id="main" style="width: 100%; height: 400px"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";

export default {
  name: "ChildComponent",
  props: {
    chartData: {
      type: Object,
      required: true,
    },
  },
  mounted() {
    this.initChart();
  },
  watch: {
    chartData: {
      handler() {
        this.initChart();
      },
      deep: true,
    },
  },
  methods: {
    initChart() {
      var chartDom = document.getElementById("main");
      var myChart = echarts.init(chartDom);
      var option;

      option = {
        xAxis: {
          type: "category",
          data: this.chartData.xAxisData, //['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        },
        yAxis: {
          type: "value",
        },
        dataZoom: [
          {
            type: "inside",
            start: 0,
            end: 100,
          },
          {
            start: 0,
            end: 100,
          },
        ],
        series: [
          {
            data: this.chartData.seriesData, //[120, 200, 150, 80, 70, 110, 130],
            type: "bar",
            label: {
              show: true,
              position: "top",
              formatter: "{c}",
            },
          },
        ],
      };
      option && myChart.setOption(option);
    },
  },
};
</script>

<style scoped>
#main {
  width: 80%;
  height: 400px;
}
</style>
